﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.Model;
using NHibernate;
using NHibernate.Criterion;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework.Common;

namespace FantastPlatform.Framework.Service
{
    public class EmployeeService : DataService<IEmployee, Employee>, IEmployeeService
    {
        public IList<IEmployeeContact> GetContactsByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeContact,EmployeeContact>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeePost> GetEmployeePostByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeePost,EmployeePost>(employeeId, sorts, pageSize, page, out totalCount);
        }

        #region IEmployeeService 成员

        
        public void QuitEmployees(List<int> ids)
        {
            ITransaction tran = null;
            ExecuteTransaction((ITransaction trans) =>
                {
                    foreach(int id in ids)
                    {
                        IEmployee employee = Session.Get<Employee>(id);
                        employee.IsInService = false;
                        employee.QuitDate = DateTime.Now;
                        Session.Update(employee);
                   }
                }, tran);
        }

        public IList<IEmployeePost> GetEmployeeRecords(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(EmployeePost)).Add(Expression.Eq("Employee.Id", employeeId));    
            ICriteria clone = criteria.Clone() as ICriteria;
            criteria = AddOrderBySorts(criteria, sorts);
            IList<IEmployeePost> list = criteria.SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize).List<IEmployeePost>();
            totalCount = int.Parse(clone.SetProjection(Projections.RowCount()).UniqueResult().ToString());
            return list;
        }

        #endregion

        #region IEmployeeService 成员
        public override object Create(IEmployee entity)
        {
            entity.IsInService = true;
            return base.Create(entity);
        }
        public override void Update(IEmployee entity)
        {
            entity.IsInService = true;

            base.Update(entity);
        }
        public override void SaveOrUpdate(IEmployee entity)
        {
            entity.IsInService = true;
            base.SaveOrUpdate(entity);
        }
        #endregion

        #region IEmployeeService 成员

        private IList<IEntity> GetOtherList<IEntity, Entity>(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(Entity)).Add(Expression.Eq("Employee.Id", employeeId))
                 .Add(Expression.Eq("Enable", true));
            ICriteria clone = criteria.Clone() as ICriteria;
            criteria = AddOrderBySorts(criteria, sorts);
            IList<IEntity> list = criteria.SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize).List<IEntity>();
            totalCount = int.Parse(clone.SetProjection(Projections.RowCount()).UniqueResult().ToString());
            return list;
        }

        public IList<IEmployeeCheckPerformance> GetEmployeeCheckPerformancesByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeCheckPerformance,EmployeeCheckPerformance>(employeeId,sorts,pageSize,page, out totalCount);
        }

        public IList<IEmployeeCheckQuarter> GetEmployeeCheckQuartersByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeCheckQuarter, EmployeeCheckQuarter>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeeCheckResult> GetEmployeeCheckResultsByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeCheckResult, EmployeeCheckResult>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeeCheckYear> GetEmployeeCheckYearsByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeCheckYear, EmployeeCheckYear>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeeEducation> GetEmployeeEducationsByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeEducation, EmployeeEducation>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeeJobExperience> GetEmployeeJobExperiencesByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeeJobExperience, EmployeeJobExperience>(employeeId, sorts, pageSize, page, out totalCount);
        }

        public IList<IEmployeePrizePunish> GetEmployeePrizePunishsByEmployeeId(int employeeId, IList<Sort> sorts, int pageSize, int page, out int totalCount)
        {
            return GetOtherList<IEmployeePrizePunish, EmployeePrizePunish>(employeeId, sorts, pageSize, page, out totalCount);
        }

        #endregion

        public override void DeleteReally(List<int> ids)
        {
            List<string> list=new List<string>();
            list.Add("Org_EmployeePost");
            list.Add("Org_EmployeeCheckPerformance");
            list.Add("Org_EmployeeCheckQuarter");
            list.Add("Org_EmployeeCheckResult");
            list.Add("Org_EmployeeCheckYear");
            list.Add("Org_EmployeeContact");
            list.Add("Org_EmployeeEducation");
            list.Add("Org_EmployeeJobExperience");
            list.Add("Org_EmployeePrizePunish");
            list.Add("Org_EmployeeUser");
            base.DeleteReallyAndRelations(ids, list);
        }
        #region IEmployeeService 成员
        #endregion




        public IEmployee Get(string code)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(Employee)).Add(Expression.Eq("Code", code))
                .Add(Expression.Eq("Enable", true));
            ICriteria clone = criteria.Clone() as ICriteria;
            IEmployee emp = criteria.List<IEmployee>().FirstOrDefault();
            return emp;
        }

     
    }
}
